﻿@{
           var action = UrlData[0].ToLower();
           var id = UrlData[1];
                                      RefData d = new RefData();
                                        BankPartner b = new BankPartner();
                                        Courier c = new Courier();
                                        
        DateTime updateDate = DateTime.Today;

            
        var data=new object[3];
        switch(action){
            case "edit":
                var refType = Request["refType"];
                var refID = Request["refId"];
                var refDesc = Request["refDesc"];
                
                var result=RefDataManager.GetRefDataValues(refType,refID,refDesc);
                if(result!=null){
                   data[0]=result;
                }
                
                break;
            case "add":
                        

                d.ParentRefType = Request["newptype"];
                d.ParentRefID = Request["newpid"];
                d.RefType = Request["newreftype"];
                d.RefID = Request["newrefid"];
                d.RefDesc = Request["newdesc"];
                d.Active = Convert.ToInt32(Request["active"]);
                d.CTCBCode = Request["ctcbcode"];
                d.CTCBDesc = Request["ctcbdesc"];
                d.UBPHCode = Request["ubphcode"];
                d.UBPHDesc = Request["ubphdesc"];
                d.PB3Code = Request["pb3code"];
                d.PB3Desc = Request["pb3desc"];
                d.PB4Code = Request["pb4code"];
                d.PB4Desc = Request["pb4desc"];
                d.PB5Code = Request["pb5code"];
                d.PB5Desc = Request["pb5desc"];
                d.PB6Code = Request["pb6code"];
                d.PB6Desc = Request["pb6desc"];
                d.PB7Code = Request["pb7code"];
                d.PB7Desc = Request["pb7desc"];
                d.PB8Code = Request["pb8code"];
                d.PB8Desc = Request["pb8desc"];
                
                data[2]=RefDataManager.CheckExistRefData(d.ParentRefType, d.ParentRefID, d.RefType, d.RefID, d.RefDesc, d.Active);
                if (data[2] == null){
                    result = RefDataManager.AddRefData(d);
                    data[0]=result;
                    
                    //data[1] = RefDataManager.GetRefDataValues( d.ParentRefType, d.ParentRefID, d.RefDesc);
                    data[1] = RefDataManager.GetRefDataValues( d.RefType, d.RefID, d.RefDesc);
                   // if (data[1] == null && Request["newptype"] != ""){
                    //    RefDataManager.AddChildData(d.ParentRefType, d.ParentRefID, d.RefDesc,d.Active);
                    //};
                };
                        break;
                 case "esave":
                    
                    d.ParentRefType = Request["newptype"];
                    d.ParentRefID = Request["newpid"];
                    d.RefType = Request["newreftype"];
                    d.RefID = Request["newrefid"];
                    d.RefDesc = Request["newdesc"];
                    d.Active = Convert.ToInt32(Request["active"]);
                    d.CTCBCode = Request["ctcbcode"];
                    d.CTCBDesc = Request["ctcbdesc"];
                    d.UBPHCode = Request["ubphcode"];
                    d.UBPHDesc = Request["ubphdesc"];
                    d.PB3Code = Request["pb3code"];
                    d.PB3Desc = Request["pb3desc"];
                    d.PB4Code = Request["pb4code"];
                    d.PB4Desc = Request["pb4desc"];
                    d.PB5Code = Request["pb5code"];
                    d.PB5Desc = Request["pb5desc"];
                    d.PB6Code = Request["pb6code"];
                    d.PB6Desc = Request["pb6desc"];
                    d.PB7Code = Request["pb7code"];
                    d.PB7Desc = Request["pb7desc"];
                    d.PB8Code = Request["pb8code"];
                    d.PB8Desc = Request["pb8desc"];
                    d.OldRefType = Request["refType"];
                    d.OldRefID = Request["refId"];
                    d.OldRefDesc = Request["refDesc"];
                   
                data[2]=RefDataManager.CheckExistRefData(d.ParentRefType, d.ParentRefID, d.RefType, d.RefID, d.RefDesc,d.Active);
                //if (data[2] == null){
                    RefDataManager.UpdateRefData(d);
                     result = RefDataManager.UpdateRefData2(d);
                    data[0]=result;
                    
                    data[1] = RefDataManager.GetRefDataValues2( d.RefType, d.RefID);
          
                //};
                

                   break;
                
                case "bpsave":
                     int mode = Convert.ToInt32(Request["mode"]);
                      b.CardExpiry = Convert.ToInt32(Request["cardexp"]);
                      b.CardLength = Convert.ToInt32(Request["cardlen"]);    
                      b.CardPrefix = Request["cardpre"];
                      b.AuthContactPerson = Request["authper"];
                      b.AuthEmail = Request["authemail"];
                      b.Active = Convert.ToInt32(Request["bactive"]);
                      b.DateCreated = updateDate;
                      b.DateUpdated = updateDate;
                      b.Code = Request["bcode"];
                      b.Name = Request["bname"];
                      b.ID = Convert.ToInt32(Request["bpid"]);
                
        
                     switch(mode){
                        case 0:
                            data[2]  = BankPartnerManager.CheckExistBP(b.Code, b.Name);
                            if (data[2]==null){
                                var newBP=BankPartnerManager.CreateBankPartner(b);
                                var searchBP = BankPartnerManager.SearchBP(b);
                                data[0] = searchBP[0];
                            
                            };
                            break;
                        case 1:
                            data[2]=null;
                            var res = BankPartnerManager.UpdateBP(b);  
                            var bpData = BankPartnerManager.GetBP(b.ID);    
                            data[0]=bpData;
                               
                            break;
                     }
                     
                   

                   break;
           
                    
                    case "cpsave":
                        int modecp = Convert.ToInt32(Request["mode"]);
                        c.Code = Request["scode"];
                        c.Name = Request["sname"];
                        c.AuthContactPerson = Request["sauthper"];
                        c.AuthEmail = Request["sauthemail"];
                        c.Active = Convert.ToInt32(Request["sactive"]);
                        c.DateCreated = updateDate;
                        c.DateUpdated = updateDate;
                        c.ID = Convert.ToInt32(Request["cpid"]);    
                        switch(modecp){
                            case 0:
                                  data[2]  = CourierManager.CheckExistCP(c.Code, c.Name);
                                    if (data[2]==null){
                                        var newCP = CourierManager.CreateCourier(c);
                                        var searchCP = CourierManager.SearchCourier(c);
                                        data[0] = searchCP[0];
                                    
                                    };
                                break;
                            case 1:
                                 data[2]=null;
                                 var res = CourierManager.UpdateCP(c);  
                                 var cpData = CourierManager.GetCourier(c.ID);    
                                 data[0]=cpData;
                                break;
                        }     

                   
                  
                   

                   break;
            }
               
             Json.Write(data,Response.Output);
    
}
